package cn.pegasus.teamwork.pegasus.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.iyizhan.teamwork.base.util.PageObject;
import cn.iyizhan.teamwork.base.util.ParameterEunm;
import cn.iyizhan.teamwork.base.util.ReMessage;
import cn.pegasus.teamwork.manager.controller.BaseManagerController;
import cn.pegasus.teamwork.pegasus.entity.BussServiceClassic;
import cn.pegasus.teamwork.pegasus.mapper.BussServiceClassicMapper;
import cn.pegasus.teamwork.pegasus.service.BussServiceClassicService;
/**
 * 项目分类管理
 * @author 40854
 *
 */
@Controller
@RequestMapping("/classic")
public class ClassicController extends BaseManagerController<BussServiceClassic, BussServiceClassicMapper> {

	private static final Logger logger = LoggerFactory.getLogger(ClassicController.class);
	
	@Autowired
	private BussServiceClassicService classicService;
	
	/**
	 * 分页查询分类列表
	 */
	@RequestMapping(value = "/list",method = {RequestMethod.GET})
	 public ModelAndView list(@RequestParam Map<String, Object> map){
		 ModelAndView view = new ModelAndView("classic/classic-list");
		 if(map.isEmpty()){
			map.put("pageData", 10);
			map.put("curPage", 1);
		}
		 try{
			 PageObject<BussServiceClassic> dicList = classicService.PageList(map);
			 view.addObject("list", dicList);
			 view.addObject("map", map);
		 }catch(Exception e){
			 logger.error("查询项目分类列表信息异常",e.getMessage());
		 }
		 return view;
	 }
	
	/**
	 * 跳转添加页面
	 */
	@RequestMapping(value="/goAdd" ,method = {RequestMethod.GET})
	 public ModelAndView goAdd(@RequestParam(value="type",required = false) String type,
			 @RequestParam(value="id",required = false) Long id){
		List<BussServiceClassic> list = classicService.findAllList(id);
		 if(null == type){
			 ModelAndView view = new ModelAndView("classic/classic-add");
			 view.addObject("list", list);
			 return view;
		 }else{
			 ModelAndView view = new ModelAndView("classic/classic-edit");
			 BussServiceClassic info = classicService.findClassicInfo(id);
			 view.addObject("info", info);
			 view.addObject("list", list);
			 return view;
		 }
	 }
	
	/**
	 * 添加
	 */
	@ResponseBody
	 @RequestMapping(value = "/add")
	 public Map<String, Object> add(@RequestParam Map<String, Object> map){
		Map<String, Object> resultMap = new HashMap<String, Object>();
       try {
    	   classicService.add(getAdmin().getId(), map);
           resultMap = ReMessage.resultBack(ParameterEunm.SUCCESSFUL_CODE, null);
       } catch (Exception e) {
           logger.error("新增项目分类异常", e);
           resultMap = ReMessage.resultBack(ParameterEunm.ERROR_500_CODE, null);
       }
       return resultMap;
	 }
	
	/**
	 * 修改
	 */
	@ResponseBody
	 @RequestMapping(value="/update")
	 public Map<String,Object> update(@RequestParam Map<String, Object> map){
		Map<String, Object> resultMap = new HashMap<String, Object>();
       try {
    	   classicService.update(getAdmin().getId(), map);
           resultMap = ReMessage.resultBack(ParameterEunm.SUCCESSFUL_CODE, null);
       } catch (Exception e) {
           logger.error("修改项目分类异常", e);
           resultMap = ReMessage.resultBack(ParameterEunm.ERROR_500_CODE, null);
       }
       return resultMap;
	 }
	/**
	 * 删除
	 */
	@ResponseBody
	 @RequestMapping(value="/delete")
	 public Map<String,Object> delete(@RequestParam(value="id",required = false) Long id){
		Map<String, Object> resultMap = new HashMap<String, Object>();
       try {
       	int i = classicService.delete(getAdmin().getId(), id);
       	if(i == 1){
       		resultMap = ReMessage.resultBack(ParameterEunm.SUCCESSFUL_CODE, null);
       	}else if(i==2){
       		resultMap = ReMessage.resultBack(ParameterEunm.ERROR_405_CODE, null);
       	}
       } catch (Exception e) {
           logger.error("删除项目分类异常", e);
           resultMap = ReMessage.resultBack(ParameterEunm.ERROR_500_CODE, null);
       }
       return resultMap;
	 }
}
